package com.xszx.servlet.admin.admin;

import com.xszx.bean.Manager;
import com.xszx.dao.ManagerDao;
import com.xszx.dao.impl.ManagerDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ManagerLoginServlet")
public class ManagerLoginServlet extends HttpServlet {
    static ManagerDao managerDao = new ManagerDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        String managerName = req.getParameter("managername");
        String managerPassword = req.getParameter("managerpassword");

        Manager manager = managerDao.login(managerName, managerPassword);

        if (manager == null) {
            //失败
            req.setAttribute("msg", "用户名或密码错误");
            resp.sendRedirect("admin/login.jsp");
        } else {
            //成功
            req.getSession().setAttribute("managerName", managerName);
            req.getSession().setAttribute("role", manager.getRole());
            req.setAttribute("manager", manager);
            if (managerPassword.equals("123456")) {
                req.setAttribute("msg", "请修改密码");
                req.getRequestDispatcher("/updatePassword.jsp").forward(req, resp);
            }
            resp.sendRedirect("admin/main.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
